package arithmetic.queue;

import java.util.Stack;

/**
 * @author jiangfeng 2019/8/17 17:27
 * 用栈实现队列
 */
public class StackQueue<E>  {

    private int total;
    private int size;

    private Stack<E> inStack;

    private Stack<E> outStack;

    public StackQueue(int size){
        inStack= new Stack<E>();
        outStack= new Stack<E>();
        this.total=size;
        this.size=0;
    }

    /**
     * 入队
     */
    public void push(E e) {
        if (size == total) {
            throw new RuntimeException("full");
        }
        inStack.push(e);
        this.size++;
    }

    /**
     * 出队
     * @return
     */
    public E pop(){
        if (size == 0) {
            throw new RuntimeException("null");
        }
        if(outStack.empty()){
            while(!inStack.empty()){
                outStack.push(inStack.pop());
            }
        }
        E pop = outStack.pop();
        size--;
        return pop;
    }


    public static void main(String[] args) {
        StackQueue stackQueue = new StackQueue(10);

        stackQueue.push("1");
        stackQueue.push("2");
        stackQueue.push("3");
        stackQueue.push("4");
        System.out.println(stackQueue.pop());
        System.out.println(stackQueue.pop());
        System.out.println(stackQueue.pop());
        stackQueue.push("5");
        stackQueue.push("6");
        System.out.println(stackQueue.pop());
        System.out.println(stackQueue.pop());
        System.out.println(stackQueue.pop());
        stackQueue.push("7");
        System.out.println(stackQueue.pop());
    }


}
